﻿using System;
using System.Diagnostics;
using Arborium;

namespace PerformanceTester
{
    public class Program
    {
        private const string TestFileName = @"c:\windows\temp\arborium_performance_test_file.arbo";
        private const int NumberOfNodes = 10000;
        private const int NumberOfIterations = 1000;

        public static void Main(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();
            TimeSpan elapsed = new TimeSpan();

            AElement tree = new AElement();
            tree.Data = AData.FromUtf8String("ROOT");
            tree.MetaData = AData.FromInt(0);

            #region Tree Creation

            stopWatch.Start();

            for (int x = 1; x < NumberOfNodes; x++)
            {
                tree.Add(
                    new AElement
                        {
                            Data = AData.FromUtf8String("Hello, node #" + x),
                            MetaData = AData.FromInt(x)
                        }
                    );
            }

            Console.WriteLine("Tree creation ({1} nodes) took {0}.", stopWatch.Elapsed.ToString(), NumberOfNodes);

            #endregion

            #region Save Tree

            stopWatch.Restart();

            for (int x = 0; x < NumberOfIterations; x++)
            {
                tree.Save(TestFileName);
            }

            elapsed = stopWatch.Elapsed;
            Console.WriteLine("{1} save operations took {0}.", elapsed.ToString(), NumberOfIterations);
            Console.WriteLine("1 save operation took {0}.", new TimeSpan(elapsed.Ticks / NumberOfIterations).ToString());

            #endregion

            #region Load Tree

            stopWatch.Restart();

            AElement loadedTree = null;

            for (int x = 0; x < NumberOfIterations; x++)
            {
                loadedTree = AElement.Load(TestFileName);
            }

            elapsed = stopWatch.Elapsed;
            Console.WriteLine("{1} Load operation took {0}.", elapsed.ToString(), NumberOfIterations);
            Console.WriteLine("1 load operation took {0}.", new TimeSpan(elapsed.Ticks / NumberOfIterations).ToString());

            #endregion

            #region Tree ToString() and Comparison

            stopWatch.Restart();

            string treeString = null;

            for (int x = 0; x < NumberOfIterations; x++)
            {
                treeString = tree.ToString();
            }

            if (loadedTree != null)
            {
                string loadedTreeString = loadedTree.ToString();

                Console.WriteLine("Result: {0}.", treeString == loadedTreeString ? "OK" : "Failed");
            }

            elapsed = stopWatch.Elapsed;
            Console.WriteLine("{1} ToString() operations took {0}.", elapsed.ToString(), NumberOfIterations);
            Console.WriteLine("1 ToString() operation took {0}.", new TimeSpan(elapsed.Ticks / NumberOfIterations).ToString());

            #endregion

            stopWatch.Stop();
        }
    }
}
